package com.ruoyi.project.service.impl;

import com.ruoyi.common.core.domain.model.TreeNode;
import com.ruoyi.project.domain.ProjectRecovery;
import com.ruoyi.project.mapper.ProjectRecoveryMapper;
import com.ruoyi.project.service.IProjectRecoveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 工程复盘目录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-12
 */
@Service
public class ProjectRecoveryServiceImpl implements IProjectRecoveryService 
{
    @Autowired
    private ProjectRecoveryMapper projectRecoveryMapper;

    /**
     * 查询工程复盘目录
     * 
     * @param recoveryId 工程复盘目录主键
     * @return 工程复盘目录
     */
    @Override
    public ProjectRecovery selectProjectRecoveryByRecoveryId(Long recoveryId)
    {
        return projectRecoveryMapper.selectProjectRecoveryByRecoveryId(recoveryId);
    }

    /**
     * 查询工程复盘目录列表
     * 
     * @param projectRecovery 工程复盘目录
     * @return 工程复盘目录
     */
    @Override
    public List<ProjectRecovery> selectProjectRecoveryList(ProjectRecovery projectRecovery)
    {
        return projectRecoveryMapper.selectProjectRecoveryList(projectRecovery);
    }

    /**
     * 新增工程复盘目录
     * 
     * @param projectRecovery 工程复盘目录
     * @return 结果
     */
    @Override
    public int insertProjectRecovery(ProjectRecovery projectRecovery)
    {
        return projectRecoveryMapper.insertProjectRecovery(projectRecovery);
    }

    /**
     * 修改工程复盘目录
     * 
     * @param projectRecovery 工程复盘目录
     * @return 结果
     */
    @Override
    public int updateProjectRecovery(ProjectRecovery projectRecovery)
    {
        return projectRecoveryMapper.updateProjectRecovery(projectRecovery);
    }

    /**
     * 批量删除工程复盘目录
     * 
     * @param recoveryIds 需要删除的工程复盘目录主键
     * @return 结果
     */
    @Override
    public int deleteProjectRecoveryByRecoveryIds(Long[] recoveryIds)
    {
        return projectRecoveryMapper.deleteProjectRecoveryByRecoveryIds(recoveryIds);
    }

    /**
     * 删除工程复盘目录信息
     * 
     * @param recoveryId 工程复盘目录主键
     * @return 结果
     */
    @Override
    public int deleteProjectRecoveryByRecoveryId(Long recoveryId)
    {
        return projectRecoveryMapper.deleteProjectRecoveryByRecoveryId(recoveryId);
    }

    @Override
    public TreeNode getTree(Long projectId) {
        List<ProjectRecovery> list = projectRecoveryMapper.getTree(projectId);
        System.out.println("list =********** " + list);
        //创建根节点
        TreeNode root = new TreeNode();
        root.setId(0L);
        root.setName("根节点");
        root.setLevel(0);
        //给根节点添加子节点
        findchild(root,list);
        //返回根节点
        return root;
    }

    @Override
    public Long selectNameByProjectRecovery(ProjectRecovery projectRecovery) {
        return projectRecoveryMapper.selectNameByProjectRecovery(projectRecovery);
    }

    @Override
    public Long getIdByPid(Long recoveryId) {
        return projectRecoveryMapper.getIdByPid(recoveryId);
    }

    @Override
    public Long getIdByProjectRecoveryDetail(Long recoveryId) {
        return projectRecoveryMapper.getIdByProjectRecoveryDetail(recoveryId);
    }

    private void findchild(TreeNode root, List<ProjectRecovery> list) {
        //list 将被转换成树结构
        //筛选出所有具有与root节点相同父ID (recoveryPid) 的ProjectRecovery对象。
        //递归调用findchild方法，传入新创建的TreeNode和原始的list。这会继续查找和添加子节点到当前节点，直到没有更多子节点为止。
        List<TreeNode> children = list.stream()
                .filter(a-> Objects.equals(root.getId(),a.getRecoveryPid()))
                .map(b->{
                    TreeNode node = new TreeNode();
                    node.setId(b.getRecoveryId());
                    node.setName(b.getRecoveryTitle());
                    node.setPid(b.getRecoveryPid());
                    node.setSort(b.getRecoverySort());
                    node.setLevel(b.getLevel());
                    findchild(node,list);
                    return node;
                })
                .collect(Collectors.toList());
        //非空就给root(自定义的tree)里的children赋值,赋的值是children
        if(!children.isEmpty()){
            root.setChildren(children);
        }
    }


}
